"""
Sentiment analysis API routes
"""

from fastapi import APIRouter, HTTPException, Query
from typing import Dict, Optional

from app.services.sentiment import sentiment_service

router = APIRouter()


@router.get("/{symbol}")
async def get_sentiment(
    symbol: str
):
    """Get sentiment analysis for symbol"""
    try:
        sentiment = await sentiment_service.get_sentiment_score(symbol)
        return sentiment
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/fear-greed-index")
async def get_fear_greed_index():
    """Get crypto market fear & greed index"""
    try:
        index = sentiment_service.calculate_fear_greed_index()
        return index
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/on-chain/{symbol}")
async def get_on_chain_metrics(
    symbol: str
):
    """Get on-chain metrics for sentiment analysis"""
    try:
        metrics = await sentiment_service.get_on_chain_metrics(symbol)
        return metrics
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/analyze-text")
async def analyze_text_sentiment(
    text: str
):
    """Analyze sentiment of provided text"""
    try:
        result = sentiment_service.analyze_text_sentiment(text)
        return result
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/social-volume/{symbol}")
async def get_social_volume(
    symbol: str,
    days: int = Query(default=7, ge=1, le=30)
):
    """Get social media volume for symbol"""
    # Mock implementation
    return {
        "symbol": symbol,
        "period_days": days,
        "twitter_mentions": 1250 * days,
        "reddit_mentions": 850 * days,
        "news_articles": 45 * days,
        "trend": "increasing" if days > 3 else "stable"
    }
